package dryworks.jsf.examples.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ResourceBundle;

import dryworks.jsf.annotations.TagAnnotation;

public class Example {
	String path; // like /dir1/dir2/file13.jspx
	String shortName; // like file13.jspx
	String shortDisplayName; // like file13
	String pathToExplanation; // like /dir1/di2/file13_Explained.jspx
	String pathWithoutExtention; // like /dir1/dir2/file13
	
	String source;
	String sourceWithRemovedAnnotations;
	List<TagAnnotation> annotations;
	LinkedHashMap<String, String> javasources;
	String facesConfigPart = null;
	ResourceBundle resourceBundle;
	List<Source> sources = null;
	
	public List<Source> getSources() {
		if (sources == null) {
			sources = new ArrayList<Source>();
			
			Source jspSource = new Source();
			jspSource.setShortName(getShortName());
			jspSource.setContent(getSourceWithRemovedAnnotations());
			
			sources.add(jspSource);
			
			for (String javaSourceName : javasources.keySet()) {
				Source javaSource = new Source();
				javaSource.setShortName(javaSourceName);
				javaSource.setContent(javasources.get(javaSourceName));
				sources.add(javaSource);
			}
			
			Source facesConfigSource = new Source();
			facesConfigSource.setShortName("faces-config.xml");
			facesConfigSource.setContent(getFacesConfigPart());
			
			sources.add(facesConfigSource);
		}
		return sources;
	}

	boolean inline = true;
	boolean needWindow = false;;

	public LinkedHashMap<String, String> getJavasources() {
		if (javasources == null) {
			javasources = new LinkedHashMap<String, String>();
		}
		return javasources;
	}

	public void setJavasources(LinkedHashMap<String, String> javasources) {
		this.javasources = javasources;
	}

	public String getFacesConfigPart() {
		return facesConfigPart;
	}

	public void setFacesConfigPart(String facesConfigPart) {
		this.facesConfigPart = facesConfigPart;
	}

	public ResourceBundle getResourceBundle() {
		return resourceBundle;
	}

	public void setResourceBundle(ResourceBundle resourceBundle) {
		this.resourceBundle = resourceBundle;
	}


	public String getShortName() {
		if (shortName == null) {
			String[] ss = path.split("/");
			String last = ss[ss.length - 1];
			shortName = last;
		}
		return shortName;
	}

	public String getPathToExplanation() {
		if (pathToExplanation == null) {
			pathToExplanation = getPath().substring(0,
					getPath().lastIndexOf('.'));
			pathToExplanation = pathToExplanation + "_Explained.jspx";
		}
		return pathToExplanation;
	}

	public String getShortDisplayName() {
		if (shortDisplayName == null) {
			shortDisplayName = getShortName().substring(0,
					getShortName().lastIndexOf('.'));
		}
		return shortDisplayName;
	}

	public String getRestUrl() {
		return "./examplesExplorer.jspx?exampleName=" + getShortName();
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getSourceWithRemovedAnnotations() {
		return sourceWithRemovedAnnotations;
	}

	public void setSourceWithRemovedAnnotations(
			String sourceWithRemovedAnnotations) {
		this.sourceWithRemovedAnnotations = sourceWithRemovedAnnotations;
	}

	public List<TagAnnotation> getAnnotations() {
		return annotations;
	}

	public void setAnnotations(List<TagAnnotation> annotations) {
		this.annotations = annotations;
	}

	public boolean isInline() {
		return inline;
	}

	public void setInline(boolean inline) {
		this.inline = inline;
	}

	public boolean isNeedWindow() {
		return needWindow;
	}

	public void setNeedWindow(boolean needWindow) {
		this.needWindow = needWindow;
	}

	public String getPathWithoutExtention() {
		if (pathWithoutExtention == null) {
			pathWithoutExtention = getPath().substring(0,
					getPath().lastIndexOf('.'));
			pathWithoutExtention = pathWithoutExtention.replace('/', '.');
		}
		return pathWithoutExtention;
	}

	public void setPathWithoutExtention(String pathWithoutExtention) {
		this.pathWithoutExtention = pathWithoutExtention;
	}
}
